import Vue from 'vue'
import VueRouter from 'vue-router'
import home from '../components/home'
import A from '../components/setting/settingMenu/A'
import B from '../components/setting/settingMenu/B'
import C from '../components/setting/settingMenu/C'
import LoadingComponent from "../components/LoadingError/LoadingComponent";
import ErrorComponent from "../components/LoadingError/ErrorComponent";
import api from "../model/api";
import fr from "element-ui/src/locale/lang/fr";

Vue.use(VueRouter)

// function lazyLoadView(AsyncView) {
//   const AsyncHandler = () => ({
//     component: AsyncView,
//     // A component to use while the component is loading.
//     loading: require('../components/LoadingError/LoadingComponent').default,
//     // A fallback component in case the timeout is exceeded
//     // when loading the component.
//     error: require('../components/LoadingError/ErrorComponent').default,
//     // Delay before showing the loading component.
//     // Default: 200 (milliseconds).
//     delay: 2000,
//     // Time before giving up trying to load the component.
//     // Default: Infinity (milliseconds).
//     timeout: 10000
//   });
//   return Promise.resolve({
//     functional: true,
//     render(h, { data, children }) {
//       // Transparently pass any props or children
//       // to the view component.
//       return h(AsyncHandler, data, children);
//     }
//   });
// }
//
//
// const discuss = () => lazyLoadView(import(/* webpackChunkName: "discuss" */'../components/functional unit/discuss'));

const router = new VueRouter({
  mode:'history',
  routes: [
    { path:'/', redirect: '/home' },
    // { path: '/home/game/:id', redirect:'/home/game/:gameName'},
    { path:'/home/setting', redirect: '/home/setting/user' },
    // { path:'/home',
    //   name: 'homeView',
    //   component: homeView,
    // },
    {
      path: '/admin',
      name:'adminHome',
      component: () => import(/* webpackChunkName: "adminHome" */'../components/admin/adminHome'),
      children: [
        {
          path: 'gameList',
          name:'gameList',
          component: () => import(/* webpackChunkName: "adminGameList" */'../components/admin/GameList'),
        },
        {
          path: 'addGame',
          name:'addGame',
          component: () => import(/* webpackChunkName: "addGame" */'../components/admin/addGame'),
        },
        {
          path: 'addMovie',
          name:'addMovie',
          component: () => import(/* webpackChunkName: "addMovie" */'../components/admin/addMovie'),
        },
        {
          path: 'addMusic',
          name:'addMusic',
          component: () => import(/* webpackChunkName: "addMusic" */'../components/admin/addMusic'),
        },
        {
          path: 'addMusicArt',
          name:'addMusicArt',
          component: () => import(/* webpackChunkName: "addMusicArt" */'../components/admin/addMusicArt'),
        },
        {
          path: 'movieList',
          name:'MovieList',
          component: () => import(/* webpackChunkName: "MovieList" */'../components/admin/MovieList'),
        },
        {
          path: 'musicList',
          name:'MusicList',
          component: () => import(/* webpackChunkName: "MusicList" */'../components/admin/MusicList'),
        },
        {
          path: 'musicArtList',
          name:'MusicArtList',
          component: () => import(/* webpackChunkName: "MusicArtList" */'../components/admin/MusicArtList'),
        },
        {
          path: 'Accusation',
          name:'accusation',
          component: () => import(/* webpackChunkName: "Accusation" */'../components/admin/accusation'),
        },
        {
          path: 'UserInfo',
          name:'userInfo',
          component: () => import(/* webpackChunkName: "UserInfo" */'../components/admin/UserInfo'),
        },
      ]
    },
    {
      path: '/',
      name:'home',
      component: home,
      children:[
        {
          path: '/search',
          name:'search',
          component: () => import('../components/mainView/Search'),
        },
        {
          path: 'home',
          name: 'homeView',
          component: () => import(/* webpackChunkName: "mainHome" */'../components/mainView/homeView'),
        },
        {
          path: 'home/setting',
          name: 'setting',
          component: () => import(/* webpackChunkName: "userSetting" */'../components/setting/userSetting'),
          children:[
            {
              path: 'user',
              name: 'settingUser',
              component: () => import(/* webpackChunkName: "settingMenu_User" */'../components/setting/settingMenu/settingMenu_User'),
            },
            {
              path: 'a',
              name: 'A',
              component: A,
            },
            {
              path: 'b',
              name: 'B',
              component: B,
            },
            {
              path: 'c',
              name: 'C',
              component: C,
            },
          ]
        },
        {
          path: 'home/hot',
          name: 'hot',
          component: () => import(/* webpackChunkName: "hotRecommend" */'../components/mainView/hotRecommend'),
        },
        {
          path: 'home/game',
          name: 'game',
          component: () => import(/* webpackChunkName: "gameView" */'../components/mainView/gameView'),
        },
        {
          path: 'home/game/:id',
          name: 'gameInfo',
          redirect: 'home/game/:id/discuss',
          component: () => import(/* webpackChunkName: "gameInfo" */'../components/mainView/InfoWindows/gameInfo'),
          children:[
            {
              path: 'discuss',
              name: 'gameDiscuss',
              component: () => import(/* webpackChunkName: "gameDiscuss" */'../components/functional unit/discuss'),
            },
            {
              path: 'gameWal',
              name: 'gameWal',
              component: () =>  import(/* webpackChunkName: "gameWal" */'../components/mainView/InfoWindows/gameChildrenRoute/gameWal'),
            },
          ]
        },
        {
          path: 'home/movie',
          name: 'movies',
          component:  () => import(/* webpackChunkName: "movieView" */'../components/mainView/moviesView'),

        },
        {
          path: 'home/movie/:id',
          name: 'movieInfo',
          redirect: 'home/movie/:id/discuss',
          component: () => import(/* webpackChunkName: "movieInfo" */'../components/mainView/InfoWindows/movieInfo'),
          children:[
            {
              path: 'discuss',
              name: 'movieDiscuss',
              component: () => import(/* webpackChunkName: "movieDiscuss" */'../components/functional unit/discuss'),
            },
            {
              path: 'movieCin',
              name: 'movieCin',
              component: () => import(/* webpackChunkName: "movieCin" */'../components/mainView/InfoWindows/movieChildrenRoute/movieCin'),
            }
          ]
        },
        {
          path: 'home/music/:id',
          name: 'musicInfo',
          redirect: 'home/music/:id/discuss',
          component: () => import(/* webpackChunkName: "musicInfo" */'../components/mainView/InfoWindows/musicInfo'),
          children:[
            {
              path: 'discuss',
              name: 'musicDiscuss',
              component: () => import(/* webpackChunkName: "musicDiscuss" */'../components/functional unit/discuss'),
            },
            {
              path: 'singerInfo',
              name:'singerInfo',
              component:() => import(/* webpackChunkName: "musicIntr" */'../components/mainView/InfoWindows/musicChildrenRoute/singerInfo'),
            },
          ]
        },

      ],
    },

  ],
  // scrollBehavior (to, from, savedPosition) {
  //   if (savedPosition) {
  //     return savedPosition
  //   } else {
  //     return { x: 0, y: 0 }
  //   }
  // },
})

router.beforeEach(async (to,from,next)=>{
  if(!sessionStorage.token){
    if(to.path === '/admin'){
      next('/home')
    }
    if(to.path === '/home/setting/user'){
      next('/home')
    }
    next()
  }else{
    if(to.path === '/admin'){
      api.judgeUserRoot(JSON.parse(sessionStorage.user).userId).then( res =>{
        console.log(res.data.data.rootCode)
        if(res.data.data.rootCode === 1 || res.data.data.rootCode === 2){
          next()
        }else{
          next('/home')
        }
      })
    }
    next()
  }
})
//
//   //to 将要访问
//   //from 从那里访问
//   //next 接着干next(url)重定向到url
// })

export default router
